/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.opu.pc.domain.domains.security.SecurityGroup;
import org.opu.pc.security.Secured;

import org.opu.pc.services.StatementGenerationUserService;
import org.opu.pc.services.beans.AbstractBean;
import org.opu.pc.ui.util.ConfigPath;
import org.opu.pc.ui.util.Options;

/**
 *
 * @author Yuriy Dunko
 */
@Secured(SecurityGroup.Secretary)
public abstract class AbstractStatementGeneration extends AbstractAction {

	private static final long serialVersionUID = -4777956314684935842L;

	static {
        JFileChooser.setDefaultLocale(ConfigPath.getInstance().getLocale());
    }

    protected StatementGenerationUserService statementGenerationService;

    public void setStatementGenerationService(StatementGenerationUserService
            statementGenerationService) {
        this.statementGenerationService = statementGenerationService;
    }


    @Override
    public void runAction(ActionEvent e) throws Exception {
        super.runAction(e);
        List<? extends AbstractBean> beans = mainView.getSelectedBeans();
        Class<? extends AbstractBean> clazz = null;
        if (beans != null && !beans.isEmpty()){
            clazz = beans.get(0).getClass();
        }
        
        if (!isAvailable(beans, clazz)){
            return;
        }

        JFileChooser c = new JFileChooser();
        c.setDialogTitle(getDialogTitle());
//        c.setDialogType(JFileChooser.FILES_ONLY);
        c.setMultiSelectionEnabled(false);
        c.addChoosableFileFilter(new FileNameExtensionFilter(
                "Microsoft Word 2007/2010 XML", "docx"));
        int rVal = c.showSaveDialog(mainView.asFrame());
        if (rVal == JFileChooser.APPROVE_OPTION) {
            File file = c.getSelectedFile();
            // TODO: docx is magic number !
            if (!file.exists() && !file.getName().endsWith(".docx")){
            	file = new File(file.getParentFile(), file.getName() + ".docx");
            }
            generateStatement(file);
            if (Options.choise(getSafety("AbstractStatementGeneration.open",
                    "Open generated statement in default editor ?"))){
                Desktop.getDesktop().open(file);
            }
        }
    }

    protected boolean isAvailable(List<?> beans, Class<?> clazz){
        return true;
    }

    protected abstract void generateStatement(File file) throws Exception;
}
